/*
 *此javaCode由YMS-JDBC替换工具生成，原mapper位置com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.mapper.DeviceListMapper
 */
package com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.mapper;

import com.yonyou.iuap.yms.api.IYmsJdbcApi;
import com.yonyou.iuap.yms.param.ResultSetProcessor;
import com.yonyou.iuap.yms.param.SQLParameter;
import com.yonyou.iuap.yms.processor.AutoBeanListProcessor;
import com.yonyou.iuap.yms.processor.AutoBeanProcessor;
import com.yonyou.iuap.yms.processor.ColumnProcessor;
import com.yonyou.iuap.yms.replace.CollectionUtil;
import com.yonyou.iuap.yms.replace.YmsSqlUtil;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.entity.DeviceListEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository
public class DeviceListMapperImpl {
    
    @Autowired
    private IYmsJdbcApi ymsJdbcApi;
    //com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.mapper.DeviceListMapper.DeleteDevice
    public int DeleteDevice(String id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update isv_devicelist\n" + 
			         "set dr = 1           \n" + 
			         "where id = ?;        \n";
        parameter.addParam(id);

        
        return ymsJdbcApi.update(sql, parameter);
    }

    public int GetDeviceListCount(String tenant_id, String deviceName, String deviceCode, String status, String type) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select count(*) from isv_devicelist ");
		sqlBuilder.append(" where ytenant_id = ? ");
		parameter.addParam(tenant_id);
		if (deviceName != null && !"".equals(deviceName)) {
			sqlBuilder.append(" and deviceName like concat('%',?,'%') ");
			parameter.addParam(deviceName);
		}
		if (deviceCode != null && !"".equals(deviceCode)) {
			sqlBuilder.append(" and deviceCode like concat('%',?,'%') ");
			parameter.addParam(deviceCode);
		}
		if (status != null && !"".equals(status)) {
			sqlBuilder.append(" and status = ? ");
			parameter.addParam(status);
		}
		if (type != null) {
			sqlBuilder.append(" and `type` = ? ");
			parameter.addParam(type);
		}
		sqlBuilder.append(" and dr = 0 ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new ColumnProcessor(Integer.class);
        return (int) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }

    public int ModifyDevice(DeviceListEntity device) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" update isv_devicelist ");
		StringBuilder trimBuilder0 = new StringBuilder();
		trimBuilder0.append(" `type` = ? ");
		parameter.addParam(device.getType());
		if (device.getDevicecode() != null && !"".equals(device.getDevicecode())) {
			trimBuilder0.append(" devicecode = ? ");
			parameter.addParam(device.getDevicecode());
		}
		if (device.getDevicename() != null && !"".equals(device.getDevicename())) {
			trimBuilder0.append(" devicename = ? ");
			parameter.addParam(device.getDevicename());
		}
		if (device.getDeviceinfo() != null && !"".equals(device.getDeviceinfo())) {
			trimBuilder0.append(" deviceinfo = ? ");
			parameter.addParam(device.getDeviceinfo());
		}
		//status为基本类型int，不需要空判断
		if (device.getStatus() != null) {
			trimBuilder0.append(" status = ? ");
			parameter.addParam(device.getStatus());
		}
		if (device.getTenant_id() != null && !"".equals(device.getTenant_id())) {
			trimBuilder0.append(" ytenant_id = ? ");
			parameter.addParam(device.getTenant_id());
		}
		if (device.getOrgId() != null && !"".equals(device.getOrgId())) {
			trimBuilder0.append(" orgId = ? ");
			parameter.addParam(device.getOrgId());
		}
		//version为基本类型int，不需要空判断
		if (device.getVersion() != null) {
			trimBuilder0.append(" version = ? ");
			parameter.addParam(device.getVersion());
		}
		if (device.getApplicationId() != null && !"".equals(device.getApplicationId())) {
			trimBuilder0.append(" applicationId = ? ");
			parameter.addParam(device.getApplicationId());
		}
		if (device.getAppCode() != null && !"".equals(device.getAppCode())) {
			trimBuilder0.append(" appCode = ? ");
			parameter.addParam(device.getAppCode());
		}
		if (device.getIpAddress() != null && !"".equals(device.getIpAddress())) {
			trimBuilder0.append(" ipAddress = ? ");
			parameter.addParam(device.getIpAddress());
		}
		if (device.getUpdateTime() != null && !"".equals(device.getUpdateTime())) {
			trimBuilder0.append(" updateTime = ? ");
			parameter.addParam(device.getUpdateTime());
		}
		sqlBuilder.append(YmsSqlUtil.trimSql(trimBuilder0, "SET", null, Collections.singletonList(","), Collections.singletonList(",")));
		sqlBuilder.append(" where id = ? and dr = 0 ");
		parameter.addParam(device.getId());

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public List<DeviceListEntity> GetDeviceListExcludeIds(String tenant_Id, List<String> IDs) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" SELECT * FROM isv_devicelist ");
		sqlBuilder.append(" WHERE ytenant_id = ? ");
		parameter.addParam(tenant_Id);
		sqlBuilder.append(" and `type` IN (1,2) ");
        if (CollectionUtil.isNotEmpty(IDs)) {
            sqlBuilder.append(" and id IN ");
            sqlBuilder.append(" ( ");
            String separator0 = " , ";
            for (int index = 0; index < IDs.size(); index++) {
                String id = IDs.get(index);
                sqlBuilder.append(" ? ");
                parameter.addParam(id);
                if (index < IDs.size() - 1) {
                    sqlBuilder.append(separator0);
                }
            }
            sqlBuilder.append(" ) ");
        }

		sqlBuilder.append(" And dr = 0 ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new DeviceListEntity());
        return (List<DeviceListEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public List<DeviceListEntity> GetDeviceList(String tenant_id, String deviceName, String deviceCode, String deviceinfo, String status, String type, String id, Integer startNumber, Integer pagesize) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" SELECT * ");
		sqlBuilder.append(" FROM isv_devicelist ");
		sqlBuilder.append(" WHERE ytenant_id = ? ");
		parameter.addParam(tenant_id);
		if (deviceName != null && !"".equals(deviceName)) {
			sqlBuilder.append(" and deviceName like concat('%',?,'%') ");
			parameter.addParam(deviceName);
		}
		if (deviceCode != null && !"".equals(deviceCode)) {
			sqlBuilder.append(" and deviceCode like concat('%',?,'%') ");
			parameter.addParam(deviceCode);
		}
		if (deviceinfo != null && !"".equals(deviceinfo)) {
			sqlBuilder.append(" and deviceinfo = ? ");
			parameter.addParam(deviceinfo);
		}
		if (status != null && !"".equals(status)) {
			sqlBuilder.append(" and status = ? ");
			parameter.addParam(status);
		}
		if (type != null) {
			sqlBuilder.append(" and `type` = ? ");
			parameter.addParam(type);
		}
		if(!id.equals("0")){ //编辑时，验证设备标识是否重复时使用，即编辑自身的编码可以重复
			sqlBuilder.append(" and id != ? ");
			parameter.addParam(id);
		}
		sqlBuilder.append(" and dr = 0 ");
		if (startNumber != null && pagesize != null) {
			sqlBuilder.append(" limit ?,? ");
			parameter.addParam(startNumber);
			parameter.addParam(pagesize);
		}

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new DeviceListEntity());
        return (List<DeviceListEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }


    public DeviceListEntity selectEntityByKey(String tenant_id, String key) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "select * from isv_devicelist where dr=0 and ytenant_id=? and deviceinfo=? limit 1;\n";
        parameter.addParam(tenant_id);
		parameter.addParam(key);

        ResultSetProcessor processor = new AutoBeanProcessor(new DeviceListEntity());
        return (DeviceListEntity) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public DeviceListEntity GetDeviceById(String tenant_id, String id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "SELECT *           \n" + 
			         "FROM isv_devicelist\n" + 
			         "WHERE ytenant_id = ?\n" +
			         "AND id = ?         \n" + 
			         "And dr = 0         \n";
        parameter.addParam(tenant_id);
		parameter.addParam(id);

        ResultSetProcessor processor = new AutoBeanProcessor(new DeviceListEntity());
        return (DeviceListEntity) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public int InsertDevice(List<DeviceListEntity> deviceList) {
		if (deviceList == null || deviceList.size()==0) return 0;
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" insert into isv_devicelist(id,`type`, devicecode, devicename, deviceinfo, status, create_time, pubts, dr, ");
		sqlBuilder.append(" tenant_id, orgId, version, applicationId, appCode,ipAddress,ytenant_id) ");
		sqlBuilder.append(" values ");
		if (CollectionUtil.isNotEmpty(deviceList)) {
			String separator0 = " , ";
			for (int index = 0; index < deviceList.size(); index++) {
				DeviceListEntity item = deviceList.get(index);
				sqlBuilder.append(" (?,?,?,?,?,?,?,?,? ");
				parameter.addParam(item.getId());
				parameter.addParam(item.getType());
				parameter.addParam(item.getDevicecode());
				parameter.addParam(item.getDevicename());
				parameter.addParam(item.getDeviceinfo());
				parameter.addParam(item.getStatus());
				parameter.addParam(item.getCreate_time());
				parameter.addParam(item.getPubts());
				parameter.addParam(item.getDr());
				sqlBuilder.append(" ,?,?,?,?,?,?,?) ");
				parameter.addParam(item.getTenant_id());
				parameter.addParam(item.getOrgId());
				parameter.addParam(item.getVersion());
				parameter.addParam(item.getApplicationId());
				parameter.addParam(item.getAppCode());
				parameter.addParam(item.getIpAddress());
				parameter.addParam(item.getYtenant_id());
				if (index < deviceList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
		}

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }

}
